package com.fan.leetcode;

import org.junit.jupiter.api.Test;

import java.util.Arrays;

/**
 * @author ZhangFan
 * @since 2021/7/16 下午 10:54
 */
public class QuestionOffer53_I {
    @Test
    public void test() {
        System.out.println("search() = " + search(new int[]{1}, 1));
    }
    public int search(int[] nums, int target) {
        /*int i = Arrays.binarySearch(nums, target);
        if (i < 0) {
            return 0;
        }
        int j = i - 1,count = 0;
        while (true) {
            if (i < nums.length && nums[i++] == target) {
                count++;
            } else {
                break;
            }
        }
        while (true) {
            if (j >= 0 && nums[j--] == target) {
                count++;
            } else {
                break;
            }
        }
        return count;*/
        int count = 0;
        for (int num : nums) {
            if (num == target) {
                count++;
            }
            if (count != 0 && num != target) {
                break;
            }
        }
        return count;
    }
}
